package com.dkd.flow.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkd.common.enums.UpdateType;
import com.dkd.common.utils.EbUtils;
import com.dkd.common.utils.SecurityUtils;
import com.dkd.flow.domain.EProcessDefinition;
import com.dkd.flow.emums.FlowType;
import com.dkd.flow.emums.IsDel;
import com.dkd.flow.emums.Status;
import com.dkd.flow.mapper.EProcessDefinitionMapper;
import com.dkd.flow.service.IProcessApproverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
@Service
public class EProcessApproverServiceImpl extends ServiceImpl<EProcessDefinitionMapper, EProcessDefinition> implements IProcessApproverService {
    @Autowired
    private EProcessDefinitionMapper eProcessDefinitionMapper;
    @Autowired
    EProcessDefinitionServiceImpl eProcessDefinitionServiceImpl;
    public JSONObject getInfo(Long id) {
        EProcessDefinition processDefinition = getById(id);
        String json = processDefinition.getJson();
        return json != null ? JSONObject.parseObject(json) : null;
    }
    // flowNo
    // flowType
    // flowName
    // flowGroup
    // flowImg
    // flowRemark
    // autoRepeat
    // myAuditAutoPass
    // remarkRequired
    // notVisibleForSponsor
    // remarkTip
    // json
    // revise
    // withdraw
    // multiPositionEnable
    // orderNum
    // status
    // delFlag
    // createBy
    // createTime
    // updateBy
    // updateTime
    // versionNumber
    // originalVersionId
    // versionCreateTime
    public JSONObject add(JSONObject approver) {
        //approver 和 processDefinition 不一样
        //approver 是json，processDefinition是完整的EProcessDefinition对象
        //如果是修改，approver的basicSetting里面有processDefinition的id
        /*basicSetting*/
        JSONObject basicSetting = approver.getJSONObject("basicSetting");
        String multiPositionEnable = basicSetting.getString("multiPositionEnable");
        String takeItYourself = basicSetting.getString("takeItYourself");
        String withdraw = basicSetting.getString("withdraw");
        String flowName = basicSetting.getString("flowName");
        String flowImg = basicSetting.getString("flowImg");
        String flowGroup = basicSetting.getString("flowGroup");
        String flowRemark = basicSetting.getString("flowRemark");
        String id = basicSetting.getString("id");
        /*processData*/
        JSONObject processData = approver.getJSONObject("processData");
        /*formData*/
        JSONObject formData = approver.getJSONObject("formData");
        String kit = formData.getString("kit");
        /*advancedSetting*/
        JSONObject advancedSetting = approver.getJSONObject("advancedSetting");
        String autoRepeat = advancedSetting.getString("autoRepeat");
        String myAuditAutoPass = advancedSetting.getString("myAuditAutoPass");
        String remarkTip = advancedSetting.getString("remarkTip");
        String remarkRequired = advancedSetting.getString("remarkRequired");
        String notVisibleForSponsor = advancedSetting.getString("notVisibleForSponsor");
        /*revise*/
        String revise = approver.getString("revise");
        //获取老的流程定义
        EProcessDefinition oldProcessDefinition = null;
        //修改
        if (id != null) {
            Long oldProcessDefinitionId = Long.parseLong(id);
            //获取老的流程定义
            oldProcessDefinition = eProcessDefinitionServiceImpl.selectEProcessDefinitionByID(oldProcessDefinitionId);
            //设置停用
            oldProcessDefinition.setStatus(Status.STOP.getValue()
                    .longValue());
            //设置删除
            oldProcessDefinition.setDelFlag(IsDel.DEL.getValue());
            //更新
            eProcessDefinitionServiceImpl.updateById(oldProcessDefinition);
        }
        EProcessDefinition newProcessDefinition = new EProcessDefinition();
        if (oldProcessDefinition != null) {
            //flowNo
            newProcessDefinition.setFlowNo(ObjectUtil.defaultIfNull(oldProcessDefinition.getFlowNo(),
                    oldProcessDefinition.getID()));
        } else {
            //flowNo
            newProcessDefinition.setFlowNo(IdUtil.getSnowflakeNextId());
        }
        //flowType
        if (kit != null) {
            for (FlowType flowType : FlowType.values()) {
                if (flowType.getName() == kit) {
                    newProcessDefinition.setFlowType(flowType.getValue()
                            .longValue());
                    break;
                }
            }
        } else {
            newProcessDefinition.setFlowType(FlowType.OTHER.getValue()
                    .longValue());
        }
        //flowName
        newProcessDefinition.setFlowName(flowName);
        //flowGroup
        newProcessDefinition.setFlowGroup(Long.parseLong(flowGroup));
        // flowImg
        newProcessDefinition.setFlowImg(Long.parseLong(flowImg));
        // flowRemark
        newProcessDefinition.setFlowRemark(flowRemark);
        // autoRepeat
        newProcessDefinition.setAutoRepeat(autoRepeat == "true" ? 1 : 0);
        // myAuditAutoPass
        newProcessDefinition.setMyAuditAutoPass(myAuditAutoPass == "true" ? 1 : 0);
        // remarkRequired
        newProcessDefinition.setRemarkRequired(remarkRequired == "true" ? 1 : 0);
        // notVisibleForSponsor
        newProcessDefinition.setNotVisibleForSponsor(notVisibleForSponsor == "true" ? 1 : 0);
        // remarkTip
        newProcessDefinition.setRemarkTip(remarkTip);
        // json
        newProcessDefinition.setJson(approver.toJSONString());
        // revise
        newProcessDefinition.setRevise(revise);
        if (withdraw != null) {
            // withdraw
            newProcessDefinition.setWithdraw(Integer.parseInt(withdraw));
        }
        if (takeItYourself != null) {
            // multiPositionEnable
            newProcessDefinition.setMultiPositionEnable(Integer.parseInt(takeItYourself));
        }
        if (multiPositionEnable != null) {
            // multiPositionEnable
            newProcessDefinition.setMultiPositionEnable(Integer.parseInt(multiPositionEnable));
        }
        // orderNum
        newProcessDefinition.setOrderNum(0L);
        // status
        newProcessDefinition.setStatus(Status.OPEN.getValue()
                .longValue());
        // delFlag
        newProcessDefinition.setDelFlag(IsDel.NOT_DEL.getValue());
        // createBy
        // createTime
        // updateBy
        // updateTime
        // versionNumber
        // originalVersionId
        /*if (oldProcessDefinition != null) {
            if (oldProcessDefinition.getOriginalVersionId() != null) {
                newProcessDefinition.setOriginalVersionId(oldProcessDefinition.getOriginalVersionId());
                newProcessDefinition.setVersionNumber(oldProcessDefinition.getVersionNumber() + 1);
            } else {
                newProcessDefinition.setOriginalVersionId(oldProcessDefinition.getID());
                newProcessDefinition.setVersionNumber(1L);
            }
        }
        //新增的时候，自己就是原始版本
        else {
            newProcessDefinition.setOriginalVersionId(newProcessDefinition.getID());
            newProcessDefinition.setVersionNumber(1L);
        }
        // versionCreateTime
        newProcessDefinition.setVersionCreateTime(new Date());*/
        newProcessDefinition.setCreateBy(SecurityUtils.getLoginUser()
                .getUser()
                .getUserName());
        if (oldProcessDefinition != null) {
            EbUtils.updateTableFields(UpdateType.ADD_FIELD,
                    newProcessDefinition);
            EbUtils.updateTableFields(UpdateType.UPDATE_FIELD,
                    newProcessDefinition);
        } else {
            EbUtils.updateTableFields(UpdateType.UPDATE_FIELD,
                    newProcessDefinition);
        }
        eProcessDefinitionServiceImpl.save(newProcessDefinition);
        //更新json
        JSONObject newJson = JSONObject.parseObject(newProcessDefinition.getJson());
        JSONObject newBasicSetting = newJson.getJSONObject("basicSetting");
        newBasicSetting.remove("id");
        newBasicSetting.put("id",
                newProcessDefinition.getID());
        JSONObject newProcessData = newJson.getJSONObject("processData");
        newProcessData.remove("processDefinitionId");
        newProcessData.put("processDefinitionId",
                newProcessDefinition.getID());
        newJson.remove("basicSetting");
        newJson.remove("processData");
        newJson.put("basicSetting",
                newBasicSetting);
        newJson.put("processData",
                newProcessData);
        newProcessDefinition.setJson(newJson.toJSONString());
        eProcessDefinitionServiceImpl.updateById(newProcessDefinition);
        return JSONObject.parseObject(newProcessDefinition.getJson());
    }
}
